package net.killerscape.globals;

import net.killerscape.util.Logger;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

public class WeaponHandler {
	private int[] weaponMap;
	private Weapon[] weapons;
	
	public int[] getAnimations(int wId) {
		int[] toReturn = {0x328, 0x337, 0x333};
		for(int i = 0; i < weaponMap.length; i++) {
			if(weaponMap[i] == wId) {
				toReturn[0] = weapons[i].animations[Weapon.STAND];
				toReturn[1] = weapons[i].animations[Weapon.WALK];
				toReturn[2] = weapons[i].animations[Weapon.RUN];
				return toReturn;
			}
		}
		return toReturn;
	}
	public void loadWeapons(String file) {
		BufferedReader in = null;
		String line;
		ArrayList<Integer> ids = new ArrayList<Integer>();
		ArrayList<Integer> numTypes = new ArrayList<Integer>();
		ArrayList<Integer> delays = new ArrayList<Integer>();
		ArrayList<Integer> interfaces = new ArrayList<Integer>();
		ArrayList<int[]> animations = new ArrayList<int[]>();
		try {
			in = new BufferedReader(new FileReader(file));
		} catch (IOException e) {
			Logger.err("Error opening weapons file");
			e.printStackTrace();
		}
		try {
			while((line = in.readLine()) != null) {
				if(line.startsWith("//")) {
					continue;
				}
				line = line.substring(line.indexOf("= ")+2);
				String[] thisLine = line.split("\t");
				
				/*
				 * Current weapon ID
				 */
				ids.add(Integer.parseInt(thisLine[0]));
				
				/*
				 * Weapon animations
				 */
				int[] animTemp = new int[8];
				animTemp[0] = (Integer.parseInt(thisLine[1]));
				if(animTemp[0] == -1)
					animTemp[0] = 0x328;
				animTemp[1] = (Integer.parseInt(thisLine[2]));
				animTemp[2] = (Integer.parseInt(thisLine[3]));
				animTemp[3] = (Integer.parseInt(thisLine[9]));
				animTemp[4] = (Integer.parseInt(thisLine[5]));
				animTemp[5] = (Integer.parseInt(thisLine[6]));
				animTemp[6] = (Integer.parseInt(thisLine[7]));
				animTemp[7] = (Integer.parseInt(thisLine[8]));
				animations.add(animTemp);
				
				/*
				 * Number of fight types
				 */
				numTypes.add(Integer.parseInt(thisLine[4]));
				
				/*
				 * Attack delay (time it takes for weapon to hit enemy after starting anim.
				 */
				delays.add(Integer.parseInt(thisLine[10]));
				
				/*
				 * Inteface ID to display for weapon
				 */
				interfaces.add(Integer.parseInt(thisLine[11]));
			}
		} catch (IOException e) {
			Logger.err("Error reading weapons file..");
			e.printStackTrace();
		}
		weapons = new Weapon[ids.size()];
		weaponMap = new int[ids.size()];
		for(int i = 0; i < ids.size(); i++) {
			weaponMap[i] = ids.get(i);
			weapons[i] = new Weapon(ids.get(i),animations.get(i),delays.get(i),numTypes.get(i),interfaces.get(i));
		}
	}
	
	public WeaponHandler() {
		loadWeapons("./data/weapons.txt");
	}
	
	static class Weapon {
		int id, fightTypes, secondsTillEnemyHit, interfaceDisplay;
		int animations[] = new int[8];
		
		public Weapon(int i, int[] anims, int secs, int fighttypes, int interfaceD) {
			id = i;
			animations = anims;
			fightTypes = fighttypes;
			secondsTillEnemyHit = secs;
		}
		
		public int standAnim() {
			return animations[STAND];
		}
		
		// Op1 = Accurate attack [Also uses this as the default attack for the set weapon]
		// Op2 = Aggressive attack
		// Op3 = Controlled attack
		// Op4 = Defensive attack
		
		public final static int STAND = 0,
								WALK = 1,
								RUN = 2,
								BLOCK = 3,
								ACCURATE_ATTACK = 4,
								AGGRESIVE_ATTACK = 5,
								CONTROLLED_ATTACK = 6,
								DEFENSIVE_ATTACK = 7;
	}
}
